﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GTA.DC.Common.UC;

namespace LanuageInfo.Forms
{
    public class PagingResult
    {
        /// <summary>
        /// 查询结果集
        /// </summary>
        public DataSet DataSet { get; set; }
        /// <summary>
        /// 查询结果行数
        /// </summary>
        public int RowCount { get; set; }
    }
    public partial class PageText : Form
    {
        /// <summary>
        /// 当前页数
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 分页大小
        /// </summary>
        private int PageSize { get; set; }

        DataSet ds = new DataSet();
        public PageText()
        {
            InitializeComponent();
            initDataSet();
            this.PageSize = 100;
            ucPageNavigation1.PageIndexChanged += new PageIndexChangedCallBack(UCPageNavigation_PageIndexChanged);
            this.PageIndex = 1;
            BindData();
        }

        private void initDataSet()
        {
            ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("NAME", typeof(string));
            for (int i = 0; i < 1000; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = "ID" + i.ToString();
                dr["NAME"] = "NAME" + i.ToString();
                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);
        }
        #region 绑定数据
        private PagingResult DataSource
        {
            get
            {
                return GetTaskPagingList(this.PageIndex, this.PageSize);
            }
        }
        #endregion 
        /// <summary>
        /// DataTable分页
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="PageIndex">页索引,注意：从1开始</param>
        /// <param name="PageSize">每页大小</param>
        /// <returns>分好页的DataTable数据</returns>              第1页        每页10条
        public  DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
        {
            if (PageIndex == 0) { return dt; }
            DataTable newdt = dt.Copy();
            newdt.Clear();
            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
            { return newdt; }

            if (rowend > dt.Rows.Count)
            { rowend = dt.Rows.Count; }
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }
        private PagingResult GetTaskPagingList(int pageIndex, int pageSize)
        {
            PagingResult obj = new PagingResult();
            DataSet dsNew = new DataSet();
            DataTable dtNew = GetPagedTable(ds.Tables[0],pageIndex,pageSize);//翻页时获取第几页的多少掉记录，这个地方用sql查询语句完成 也可以在这个地方加查询条件
            dsNew.Tables.Add(dtNew);
            obj.DataSet = dsNew;
            obj.RowCount = ds.Tables[0].Rows.Count;//翻页时获取数据总数，这个地方用sql查询语句完成
            return obj;
        }
        /// <summary>
        /// 绑定数据列表
        /// </summary>
        public void BindData()
        {
            PagingResult result = this.DataSource;
            ucPageNavigation1.PageCount = result.RowCount % this.PageSize == 0 ? result.RowCount / this.PageSize : result.RowCount / this.PageSize + 1;
            ucPageNavigation1.CurrentPage = this.PageIndex;
            DataSet ds = result.DataSet;
            if (ds != null && ds.Tables.Count > 0)
            {
                gridControl1.DataSource = ds.Tables[0];
            }
        }
        #region 分页事件
        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="currentPage"></param>
        void UCPageNavigation_PageIndexChanged(int currentPage)
        {
            if (this.PageIndex != currentPage)
            {
                this.PageIndex = currentPage;
                BindData();
            }
        }
        #endregion
    }
    
}
